from pydantic import BaseModel
from typing import Optional, List
from datetime import datetime
from app.models.exam import ExamStatus, ExamType


class ExamBase(BaseModel):
    title: str
    description: Optional[str] = None
    duration: int  # 分钟
    total_score: int = 100
    pass_score: int = 60
    start_time: datetime
    end_time: datetime
    exam_type: ExamType = ExamType.FORMAL
    is_shuffle: bool = True
    allow_review: bool = True


class ExamCreate(ExamBase):
    pass


class ExamUpdate(BaseModel):
    title: Optional[str] = None
    description: Optional[str] = None
    duration: Optional[int] = None
    total_score: Optional[int] = None
    pass_score: Optional[int] = None
    start_time: Optional[datetime] = None
    end_time: Optional[datetime] = None
    status: Optional[ExamStatus] = None
    is_shuffle: Optional[bool] = None
    allow_review: Optional[bool] = None


class ExamResponse(ExamBase):
    id: int
    status: ExamStatus
    question_count: int
    created_by: int
    created_at: datetime
    updated_at: Optional[datetime] = None
    creator: dict  # 创建者信息

    class Config:
        from_attributes = True


class ExamList(BaseModel):
    total: int
    items: List[ExamResponse]


class ExamForStudent(BaseModel):
    id: int
    title: str
    description: Optional[str] = None
    duration: int
    total_score: int
    start_time: datetime
    end_time: datetime
    exam_type: ExamType
    question_count: int
    
    class Config:
        from_attributes = True